package asteroids.model.programs.parsing.expressions;

import java.util.List;
import java.util.Map;

import asteroids.model.Ship;
import asteroids.model.programs.parsing.types.*;

/**
 * A class of equal expressions.
 * An equal expression returns true if and only if the left-hand operand and the right-hand operand are equal.
 * 
 * @author Thijs Dieltjens - Wim Konings
 * @version 2.0
 */
public class EqualExpr extends LogicalExpr {

	public EqualExpr(Expression e1, Expression e2,int line, int column) {
		super(e1, e2,line, column);
	}

	@Override
	public BooleanLiteral eval(Ship ship, Map<String, Type> globals) {
		if(getLeftOperand().equals(getRightOperand(), ship) ){
			return new BooleanLiteral(true);
		}
		return new BooleanLiteral(false);
	}

	@Override
	public  List<String> getTypeCheckErrors(List<String> errors, Map<String, Type> globals) {
		errors = getLeftOperand().getTypeCheckErrors(errors, globals);
		errors = getRightOperand().getTypeCheckErrors(errors, globals);
		try{
			if((getLeftOperand().eval(null, globals).getClass()) != getRightOperand().eval(null, globals).getClass()){
				errors.add(getLine()+"-"+getColumn()+": This comparison must be between two expressions of the same type.");
			}	
		}
		catch(NullPointerException e){
			errors.add(getLine()+"-"+getColumn()+": The comparision has recieved invalid arguments.");
		}
		return errors;
	}
}
